package com.example.administrator.szb.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.bean.TmpZxBean;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.view.MyListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class FastZXActivity extends MVPBaseActivity {
    MyListView listView1,listView2;
    TextView tv_yestoday,tv_today,bar_title;
    private List<TmpZxBean.ListBean> list1 = new ArrayList<>();
    private List<TmpZxBean.ListBean> list2 = new ArrayList<>();
    private String today = "";
    private String yestoday = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_fast_zx);

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        bar_title = findViewById(R.id.bar_title);
        bar_title.setText("快讯");
        listView1 = findViewById(R.id.listView1);
        listView2 = findViewById(R.id.listView2);
        tv_today = findViewById(R.id.tv_today);
        tv_yestoday = findViewById(R.id.tv_yestoday);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        getDates();
        getZx();
    }
    public void getZx(){
        String url = "http://119.29.63.230/24h/news.json?newsid=0";
        HttpUtils.get2(activity,url, 0, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                getDates();
                TmpZxBean l = JSON.parseObject(str,new TypeReference<TmpZxBean>(){});
                for (int i=0;i<l.getList().size();i++){
                    if (l.getList().get(i).getTime().contains(today)){
                        list1.add(l.getList().get(i));
                    }else {
                        list2.add(l.getList().get(i));
                    }
                }
                initViews();
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }
    private void initViews(){
        listView1.setAdapter(new MyAdapter(context,list1));
        listView2.setAdapter(new MyAdapter(context,list2));
    }


    class MyAdapter  extends BaseAdapter {
        private Context context;
        private List list;
        LayoutInflater inflater;
        public MyAdapter(Context context, List list) {
            this.context = context;
            this.list = list;
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public  Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView==null){
                convertView = inflater.inflate(R.layout.item_fastzx,null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.v_line.setVisibility(View.VISIBLE);
            if (position==list.size()-1){
                holder.v_line.setVisibility(View.INVISIBLE);
            }
            TmpZxBean.ListBean listBean = (TmpZxBean.ListBean) getItem(position);
            holder.tv_content.setText(listBean.getContent());
            String[] str = listBean.getTime().split(" ");
            String[] str2 = str[1].split(":");
            holder.tv_time.setText(str2[0]+":"+str2[1]);
            return convertView;
        }

        class ViewHolder{
             View v_line;
             TextView tv_time;
             TextView tv_content;
            public ViewHolder(View view){
                v_line = (View) view.findViewById(R.id.v_line);
                tv_time = (TextView) view.findViewById(R.id.tv_time);
                tv_content = (TextView) view.findViewById(R.id.tv_content);
            }
        }
    }





    private void getDates(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();//取时间
        today = formatter.format(date);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动
        date=calendar.getTime(); //这个时间就是日期往后推一天的结果

        yestoday = formatter.format(date);
        tv_today.setText("今天("+today+")");
        tv_yestoday.setText("昨天("+yestoday+")");
    }
}
